package com.monkey.demo.controller;

import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;
import com.monkey.bean.PageResponse;
import com.monkey.bean.Result;
import com.monkey.demo.excel.SysUserExport;
import com.monkey.redis.util.RedisUtil;
import com.monkey.demo.entity.SysUser;
import com.monkey.demo.entity.req.SysUserReq;
import com.monkey.demo.service.SysUserService;
import com.monkey.tool.DateUtils;
import com.monkey.tool.ExportWordUtil;
import com.monkey.tool.checker.fun.Predicate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * (SysUser)表控制层
 *
 * @author makejava
 * @since 2023-11-25 22:23:53
 */
@RestController
@RequestMapping("sysUser")
@Api(value = "系统用户控制",tags = "系统用户控制",description = "代码生成器生成的代码")
@Slf4j
public class SysUserController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SysUserExport sysUserExport;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping
    @ApiOperation(value = "分页查询信息",notes = "分页查询")
    public Result<PageResponse<SysUser>> queryByPage(SysUserReq sysUserReq) {
        return Result.ok(this.sysUserService.queryByPage(sysUserReq));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result<SysUser> queryById(@PathVariable("id") Long id) {
        return Result.ok(this.sysUserService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param sysUser 实体
     * @return 新增结果
     */
    @PostMapping
    public Result<SysUser> add(SysUser sysUser) {
        return Result.ok(this.sysUserService.insert(sysUser));
    }

    /**
     * 编辑数据
     *
     * @param sysUser 实体
     * @return 编辑结果
     */
    @PutMapping
    public Result<SysUser> edit(SysUser sysUser) {
        return Result.ok(this.sysUserService.update(sysUser));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public Result<Boolean> deleteById(Long id) {
        return Result.ok(this.sysUserService.deleteById(id));
    }

    @GetMapping("/testRedis")
    @ApiOperation(value = "测试reis",notes = "测试reis")
    public void testRedis(){
        redisUtil.set(redisUtil.buildKey("name","2"),"红旗飘飘!");
        log.info("输出的信息为{}",redisUtil.get(redisUtil.buildKey("name","2")));
        redisUtil.del(redisUtil.buildKey("name","2"));
    }
    @ApiOperation(value = "导出excel", notes = "导出excel")
    @GetMapping("exportData")
    public void exportData() {
        //指定数据条件
        Map<String, Object> map = new HashMap<>();
        sysUserExport.exportWithBigData("用户数据",map);
    }

    @ApiOperation(value = "freemarker导出Word文档", notes = "freemarker导出Word文档")
    @GetMapping("exportWord")
    @Transactional
    public void exportWord() throws Exception {
        Preconditions.checkArgument(StringUtils.isNotBlank("11"),"返回true,异常不会抛!");
        //指定数据条件
        Map<String, Object> map = new HashMap<>();
        map.put("name","张三");
        map.put("date", DateUtils.getDateStr(new Date()));
        ExportWordUtil.exportWord(map,"文档导出","WordExport.ftl");
    }

}

